﻿using DevExpress.XtraCharts;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.mMachine.Common
{
    public class ChartControlHelper
    {
        public static void setData_Chart_BarLine(ChartControl chart, DataTable data, string colums_xName, string colums_xaxis, string colums_yaxis, int flag)
        {
            try
            {
                if (data.Rows.Count > 0 && data != null)
                {
                    chart.Series.Clear();
                    Series seriesline = new Series(colums_xaxis, ViewType.Bar);
                    Series series = new Series(colums_yaxis, ViewType.Line);
                    seriesline.DataSource = data;
                    chart.Series.Add(seriesline);
                    SetSeries(seriesline, colums_xName, new string[] { colums_xaxis });
                    LineSeriesView lineView = (LineSeriesView)series.View;
                    lineView.Color = Color.Blue;
                    series.DataSource = data;
                    chart.Series.Add(series);
                    SetSeries(series, colums_xName, new string[] { colums_yaxis });
                    LineSeriesView LineView1 = (LineSeriesView)series.View;
                    if (flag == 2)
                        LineView1.Color = Color.Green;
                    else if (flag == 3)
                        LineView1.Color = Color.Orange;
                    else if (flag == 4)
                        LineView1.Color = Color.Cyan;
                    else if (flag == 5)
                        LineView1.Color = Color.Red;
                    else
                        LineView1.Color = Color.Violet;
                    //setting show percent 
                    seriesline.PointOptions.ValueNumericOptions.Format = NumericFormat.General;
                    series.PointOptions.ValueNumericOptions.Format = NumericFormat.Percent;
                    //add y axis show
                    XYDiagram diagramhour = (XYDiagram)chart.Diagram;
                    diagramhour.SecondaryAxesY.Clear();
                    SecondaryAxisY AxisY = new SecondaryAxisY();
                    diagramhour.SecondaryAxesY.Add(AxisY);
                    ((LineSeriesView)series.View).AxisY = AxisY;
                    //设置右侧Y轴按百分比显示
                    AxisY.Label.TextPattern = "{V:0%}";
                    //series title positive setting
                    DevExpress.XtraCharts.Legend Legends = (DevExpress.XtraCharts.Legend)chart.Legend;
                    Legends.Visibility = DevExpress.Utils.DefaultBoolean.True;
                    Legends.AlignmentVertical = LegendAlignmentVertical.TopOutside;
                    Legends.AlignmentHorizontal = LegendAlignmentHorizontal.Center;
                    Legends.Direction = LegendDirection.LeftToRight;
                    Legends.TextVisible = true;
                    diagramhour.AxisY.ConstantLines.Clear();

                    BarSeriesLabel point = (BarSeriesLabel)seriesline.Label;
                    point.Position = BarSeriesLabelPosition.TopInside;

                }
            }
            catch (Exception ex)
            {

            }
        }
        public static void SetSeries(Series series, string argumentDataMember, string[] valueDataMember)
        {
            // setting X  axis type
            series.ArgumentScaleType = ScaleType.Qualitative;
            //show information and data
            series.PointOptions.PointView = PointView.Values;
            // setting X axis bind field
            series.ArgumentDataMember = argumentDataMember;
            series.ValueScaleType = ScaleType.Numerical;
            // setting Y axis bind field
            series.ValueDataMembers.AddRange(valueDataMember);
            // show lable data
            series.Label.Visible = true;

        }
        public static void setData_Chart_Bar(ChartControl chart, DataTable data, string colums_xName, string colums_xaxis, int flag)
        {
            try
            {

                if (data.Rows.Count > 0 && data != null)
                {
                    chart.Series.Clear();
                    Series seriesline = new Series(colums_xaxis, ViewType.Bar);               

                    seriesline.DataSource = data;
                    chart.Series.Add(seriesline);
                    SetSeries(seriesline, colums_xName, new string[] { colums_xaxis });

                    //setting show percent 
                    seriesline.PointOptions.ValueNumericOptions.Format = NumericFormat.General;

                    //setting view color
                    SideBySideBarSeriesView sideBySide = new SideBySideBarSeriesView();
                    sideBySide.Color = Color.Green;
                    
                    //add y axis show
                    //XYDiagram diagramhour = (XYDiagram)chart.Diagram;
                    //diagramhour.SecondaryAxesY.Clear();
                    //SecondaryAxisY AxisY = new SecondaryAxisY();
                    //diagramhour.SecondaryAxesY.Add(AxisY);
                    //((LineSeriesView)series.View).AxisY = AxisY;
                    //series title positive setting
                    DevExpress.XtraCharts.Legend Legends = (DevExpress.XtraCharts.Legend)chart.Legend;
                    Legends.Visibility = DevExpress.Utils.DefaultBoolean.True;
                    Legends.AlignmentVertical = LegendAlignmentVertical.TopOutside;
                    Legends.AlignmentHorizontal = LegendAlignmentHorizontal.Center;
                    Legends.Direction = LegendDirection.LeftToRight;
                    Legends.TextVisible = true;
                   // diagramhour.AxisY.ConstantLines.Clear();

                    BarSeriesLabel point = (BarSeriesLabel)seriesline.Label;
                    point.Position = BarSeriesLabelPosition.TopInside;

                }
            }
            catch (Exception ex)
            {

            }
        }

        public static void setData_Chart(ChartControl chart, ViewType vt, DataTable data, string colums_xaxis, string colums_yaxis)
        {
            try
            {

                if (data.Rows.Count > 0 && data != null)
                {
                    chart.Series.Clear();
                    Series seriesAuto = new Series("", vt);
                    foreach (DataRow dr in data.Rows)
                    {
                        SeriesPoint p = new SeriesPoint();

                        //原值显示
                        if (dr[colums_xaxis].ToString() != "")
                        {
                            double[] d = new double[] { double.Parse(Math.Round((decimal.Parse(dr[colums_yaxis].ToString())), 0).ToString()) };
                            p.Values = d;
                            p.Argument = dr[colums_xaxis].ToString();
                            seriesAuto.Points.Add(p);
                        }
                    }
                    chart.Series.Add(seriesAuto);

                    chart.Legend.Visibility = DevExpress.Utils.DefaultBoolean.False;
                    DevExpress.XtraCharts.Legend Legends = (DevExpress.XtraCharts.Legend)chart.Legend;
                    Legends.Visibility = DevExpress.Utils.DefaultBoolean.True;
                    Legends.AlignmentVertical = LegendAlignmentVertical.TopOutside;//top
                    Legends.AlignmentHorizontal = LegendAlignmentHorizontal.Center;//left
                    Legends.Direction = LegendDirection.LeftToRight;
                    Legends.TextVisible = true;

                    //强制显示所有x轴标签
                    //XYDiagram diagram = ((XYDiagram)chart.Diagram);
                    //diagram.AxisX.QualitativeScaleOptions.AutoGrid = false;
                    //diagram.AxisX.Label.ResolveOverlappingOptions.AllowHide = false;

                    //设置Series的标签
                    seriesAuto.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True;//每个点都显示数据
                    seriesAuto.Label.PointOptions.PointView = PointView.ArgumentAndValues;
                    seriesAuto.ToolTipEnabled = DevExpress.Utils.DefaultBoolean.True;  // 设置鼠标悬浮显示toolTip
                    //设置标签的显示位置
                    //SideBySideBarSeriesLabel label = (SideBySideBarSeriesLabel)seriesAuto.Label;
                    //label.Position = BarSeriesLabelPosition.Top;
                }
            }
            catch
            {

            }
        }

    }
}
